package com.david.shortcuts

import android.app.KeyguardManager
import android.app.Service
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.Toast

class ShortCutsProxyService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.e("service", "${intent?.getIntExtra(SHORT_CUTS_KEY, -1)}")
        when (intent?.getIntExtra(SHORT_CUTS_KEY, -1)) {
            SHORT_CUTS_ALISCAN -> {
                startPage(this, "alipays://platformapi/startapp?saId=10000007")
            }
            SHORT_CUTS_ALIPAY_QR -> {
                startPage(this, "alipays://platformapi/startapp?saId=20000056")
            }
            SHORT_CUTS_CITY_TRAFFIC -> {
                startPage(this, "smkapp://scanCodeRide2")
            }
            SHORT_CUTS_WECHAT_SCAN -> {
                intent.component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                intent.putExtra("LauncherUI.From.Scaner.Shortcut", true)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.action = "android.intent.action.VIEW"
                startActivity(intent)
            }


            SHORT_CUTS_ONE_KEY_LOCK -> {
                Log.e("shortcuts", "SHORT_CUTS_ONE_KEY_LOCK...")
                lockscreen()
            }
        }
        startForeground(0, null)
        return START_STICKY
    }


    /**
     * 一键锁屏
     */
    private fun lockscreen() {
        val dpm = getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager
        val who = ComponentName(this, MyAdmin::class.java)
        if (dpm.isAdminActive(who)) {
            dpm.lockNow() // 锁屏
            //dpm.resetPassword("", 0) // 设置屏蔽密码
            // 清除Sdcard上的数据
            // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
            // 恢复出厂设置
            // dpm.wipeData(0);
        } else {
            Toast.makeText(this, "还没有打开管理员权限", Toast.LENGTH_LONG).show()
            return
        }

        //val keyGard = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
        //keyGard.
    }

    companion object {
        const val SHORT_CUTS_KEY = "SHORT_CUTS_KEY"
        const val SHORT_CUTS_ALISCAN = 0
        const val SHORT_CUTS_ALIPAY_QR = 1
        const val SHORT_CUTS_CITY_TRAFFIC = 2
        const val SHORT_CUTS_WECHAT_SCAN = 3


        const val SHORT_CUTS_ONE_KEY_LOCK = 100

        fun startPage(context: Context, uri: String) {
//            Intent(Intent.ACTION_VIEW, Uri.parse("smkapp://scanCodeRide2"))
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }
}